package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

import static common.Utils.solveAlertAccept;

public class NoLoginPage {
    public static String loginUrl = "http://8.138.167.159:8080/blog_login.html";
    public static String listUrl = "http://8.138.167.159:8080/blog_list.html";
    public static String detailsUrl = "http://8.138.167.159:8080/blog_detail.html?blogId=2";
    public static String editUrl = "http://8.138.167.159:8080/blog_update.html?blogId=2";
    public static String publishUrl = "http://8.138.167.159:8080/blog_edit.html";
    public static WebDriver driver = Utils.createWebdriver();
    /**
     * loginFailByUserName
     * 验证用户名不存在
     * 通过登录页面的url确认
     */
    public static void loginFailByUserName() throws InterruptedException {
        driver.get(loginUrl);
        driver.findElement(By.cssSelector("#username")).sendKeys("2222");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();
        solveAlertAccept();
        String url = driver.getCurrentUrl();
        url.equals("http://8.138.167.159:8080/blog_login.html");
        driver.navigate().refresh();
    }

    /**
     * loginFailByPassword
     * 验证密码错误
     * 通过登录页面的url确认
     */
    public static void loginFailByPassword() {
        driver.get(loginUrl);
        driver.findElement(By.cssSelector("#username")).sendKeys("123");
        driver.findElement(By.cssSelector("#password")).sendKeys("1234");
        driver.findElement(By.cssSelector("#submit")).click();
        solveAlertAccept();
        String url = driver.getCurrentUrl();
        url.equals("http://8.138.167.159:8080/blog_login.html");
        driver.navigate().refresh();
    }

    /**
     * listNoLoginByLoginPage
     * 通过登录页面的主页按钮进入博客列表
     * 通过登录页面的url确认
     */
    public static void listNoLoginByLoginPage() {
        driver.get(loginUrl);
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
        String url = driver.getCurrentUrl();
        url.equals("http://8.138.167.159:8080/blog_login.html");
    }

    /**
     * listNoLoginByUrl
     * 直接通过url的方式进入
     * 通过登录页面的url确认
     */
    public static void listNoLoginByUrl() {
        driver.get(listUrl);
        String url = driver.getCurrentUrl();
        url.equals("http://8.138.167.159:8080/blog_login.html");
    }

    /**
     * detailsNoLoginByUrl
     * 直接通过url的方式进入
     * 通过登录页面的url确认
     */
    public static void detailsNoLoginByUrl() {
        driver.get(detailsUrl);
        String url = driver.getCurrentUrl();
        url.equals("http://8.138.167.159:8080/blog_login.html");
    }

    /**
     * editNoLoginByUrl
     * 直接通过url的方式进入
     * 通过登录页面的url确认
     */
    public static void editNoLoginByUrl() {
        driver.get(editUrl);
        String url = driver.getCurrentUrl();
        url.equals("http://8.138.167.159:8080/blog_login.html");
    }

    /**
     * publishNoLoginByLoginPage
     * 通过登录页面的写博客按钮进入博客列表
     * 通过登录页面的url确认
     */
    public static void publishNoLoginByLoginPage() {
        driver.get(loginUrl);
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        String url = driver.getCurrentUrl();
        url.equals("http://8.138.167.159:8080/blog_login.html");
    }

    /**
     * publishNoLoginByUrl
     * 直接通过url的方式进入
     * 通过登录页面的url确认
     */
    public static void publishNoLoginByUrl() {
        driver.get(publishUrl);
        String url = driver.getCurrentUrl();
        url.equals("http://8.138.167.159:8080/blog_login.html");
        driver.navigate().back();
    }
}
